﻿using System;
using System.IO;

namespace Rosetta.Default.AddIns.IO
{
    public sealed class FileConverter : Converter
    {
        #region Constructors

        public FileConverter()
            : base("File Converter")
        {
            Category = "IO";

            Add<FileInfo, byte[]>("GetBytes", GetBytes, "file");
            Add<FileInfo, string[]>("GetLines", GetLines, "file");
            Add<FileInfo, string>("GetText", GetText, "file");
        }

        #endregion

        #region Class Methods

        private static byte[] GetBytes([NotNull] FileInfo file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            return File.ReadAllBytes(file.FullName);
        }

        private static string[] GetLines([NotNull] FileInfo file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            return File.ReadAllLines(file.FullName);
        }

        private static string GetText([NotNull] FileInfo file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            return File.ReadAllText(file.FullName);
        }

        #endregion
    }
}